package edu.boun.g4.coursity.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.Initiative;

/**
 * 
 * @author mertusta
 * @author bulutkorkmaz
 * @author alifindik
 * 
 */
@Service("initiativeService")
@Transactional
public class InitiativeService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all initiatives
	 * 
	 * @return a list of initiatives
	 */
	@SuppressWarnings("unchecked")
	public List<Initiative> getAll() {
		logger.debug("Retrieving all initiatives");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Initiative");

		// Retrieve all
		return query.list();
	}

	/**
	 * Retrieves initiative of a course
	 */
	public Initiative getAll(Integer courseId) {
		logger.debug("Retrieving initiative of the course");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session
				.createQuery("FROM Course as p LEFT JOIN FETCH  p.initiative WHERE p.id="
						+ courseId);

		Course course = (Course) query.uniqueResult();

		// Retrieve all
		return course.getInitiative();
	}

	/**
	 * Retrieves a single initiative
	 */
	public Initiative get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing initiative first
		Initiative initiative = (Initiative) session.get(Initiative.class, id);

		return initiative;
	}

	/**
	 * Retrieves a single Initiative by its info
	 */
	public Initiative getByInfo(String info) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM Initiative WHERE info='" + info
				+ "'");

		Initiative initiative = (Initiative) query.uniqueResult();

		return initiative;
	}

	/**
	 * Adds a new initiative
	 */
	public void add(Initiative initiative) {
		logger.debug("Adding new initiative");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(initiative);
	}

	/**
	 * Deletes an existing initiative
	 * 
	 * @param id
	 *            the id of the existing initiative
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing initiative");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing initiative first
		Initiative initiative = (Initiative) session.get(Initiative.class, id);

		// Delete
		session.delete(initiative);
	}

	/**
	 * Edits an existing initiative
	 */
	public void edit(Initiative initiative) {
		logger.debug("Editing existing initiative");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing initiative via id
		Initiative existingInitiative = (Initiative) session.get(
				Initiative.class, initiative.getId());

		// Assign updated values to this initiative
		existingInitiative.setInitiativeName(initiative.getInitiativeName());

		// Save updates
		session.save(existingInitiative);
	}
}
